package Rainwater.controller;

import Rainwater.Service.PointService;
import Rainwater.baen.vo.PointVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;

/**
 * @author cfy
 * @date 2024/10/16
 **/
@Controller
@RequestMapping("/rainwater/page")
public class PointController {
//    private R r = new R();
    @Autowired
    PointService pointService;
    @GetMapping("/point")
    public ModelAndView pageIndex(
            @RequestParam(value = "pageNumber",required = false) Integer pageNumber,
            @RequestParam(value = "search",required = false) String search){
        Integer upPage = 1;
        Integer downPage = 2;
        if (pageNumber==null||pageNumber<2){
            pageNumber = 1;
        }else {
            upPage = pageNumber-1;
            downPage = pageNumber +1;
        }
        ArrayList<PointVo> points = pointService.getPoints(pageNumber,search);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("data_point");
        modelAndView.addObject("points",points);
        modelAndView.addObject("upPage",upPage);
        modelAndView.addObject("downPage",downPage);
        modelAndView.addObject("search",search);
        return modelAndView;
    }
    @GetMapping("/point/delete")
    public RedirectView delateUser(@RequestParam("id") Integer id){
        pointService.deletePoint(id);
        RedirectView redirectView = new RedirectView();
        redirectView.setUrl("/rainwater/page/point");
        return redirectView;
    }
    @GetMapping("/point/select")
    public void selectPoint(@RequestParam("id") Integer id){
        //todo 详情页面
        System.out.println("select"+id);
        PointVo pointById = pointService.getPointById(id);
    }
    @PostMapping("/point/search")
    public RedirectView searchAll(@RequestParam("search") String search){
//        String url = null;
        RedirectView redirectView = new RedirectView();
        try {
            redirectView.setUrl("/rainwater/page/point?pageNumber=1&search="+
                    URLEncoder.encode(search,"UTF-8"));
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
        return redirectView;
    }

}
